//
//  YWSingleWordTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSingleWordTableViewCell.h"

@implementation YWSingleWordTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.keyLabel = [[UILabel alloc] init];
        _keyLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_keyLabel];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _keyLabel.frame = CGRectMake(20, 10, SCREENWIDTH, 20);
}

- (void)setPicModel:(YWSearchPicModel *)picModel
{
    if (_picModel != picModel) {
        _picModel = picModel;
        
        _keyLabel.text = picModel.keyword;
    }
}

//将汉字一个一个存到数组中
- (NSMutableArray *)isChinese:(NSString *)str
{
    NSMutableArray *chineseArray = [NSMutableArray array];
    for (int i = 0; i < [str length]; i++) {
        //将汉字转化为数字
        int num = [str characterAtIndex:i];
        //将字符中汉字添加到数组
        if (num > 0x4e00 && num < 0x9fff) {
            [chineseArray addObject:[str substringWithRange:(NSRange){i ,1}]];
        }
    }
    return chineseArray;
}

- (void)setSearchBarString:(NSString *)searchBarString
{
    _searchBarString = searchBarString;
    
    NSString *searchString = searchBarString;
    NSRange range;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.picModel.keyword];
    NSStringCompareOptions mask = NSCaseInsensitiveSearch | NSNumericSearch;
    
    if ([self isChinese:searchBarString].count) {
        for (NSString *chinese in [self isChinese:searchBarString]) {
            range = [_picModel.keyword rangeOfString:chinese options:mask];
            
            while (range.length) {
                [attributedString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:14]} range:range];
                range = [_picModel.keyword rangeOfString:chinese options:mask range:(NSRange){range.location + range.length, _picModel.keyword.length - range.location - range.length}];
            }
            
            searchString = [searchString stringByReplacingOccurrencesOfString:chinese withString:@""];
        }
    }
    
    searchString = [searchString stringByReplacingOccurrencesOfString:@" " withString:@""];
    range = [_picModel.keyword rangeOfString:searchString options:mask];
    
    while (range.length) {
        [attributedString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:14]} range:range];
        range = [_picModel.keyword rangeOfString:searchString options:mask range:(NSRange){range.location + range.length, _picModel.keyword.length - range.location - range.length}];
    }
    
    _keyLabel.attributedText = attributedString;
    
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
